/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.server.servlet.book.provider;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import offset.nodes.client.dialog.io.model.BookType;

/**
 *
 * @author Walter L�tgenau
 */
public class UserAgentRequestWrapper extends HttpServletRequestWrapper implements HttpServletRequest {
    private static final String HEADER_USER_AGENT = "user-agent";
    private static final String PREFIX_USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120403211507 ";
    private static final String BOOK_USER_AGENT = "Book";
    private BookType bookType = BookType.epub;

    public UserAgentRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    public UserAgentRequestWrapper(BookType bookType, HttpServletRequest request) {
        this(request);
        this.bookType = bookType;
    }

    @Override
    public String getHeader(String name) {
        if (name.toLowerCase().equals(HEADER_USER_AGENT))
            return getUserAgent();
        return super.getHeader(name);
    }

    /**
     * Create an artificial user agent name from the book type.
     *
     * This can be used to have specific CSS for the book export.
     *
     * @return the artificial user agent name;
     */
    protected String getUserAgent() {
        String ua = bookType.toString();
        ua = ua.substring(0, 1).toUpperCase() + ua.substring(1);

        return PREFIX_USER_AGENT + BOOK_USER_AGENT + ua;
    }

}
